const async           = require('async');
const BaseHandler     = require('../base-handler');
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class MarketingProbeHandler extends BaseHandler {

    get pattern() {
        return "/mkprobe/:url/:wall_id/:asset_id";
    }

    doGET(request, response) {
        let wallId = request.params.wall_id;
        let assetId = request.params.asset_id;
        let url = request.params.url;
        url = url.replace(/-/g, '/');
        url = atob(url);
        var me = this;
        
        if (request.session.walls && !request.session.walls[wallId]) {
            request.session.walls[wallId] = {};
        }

        async.waterfall([
            (callback) => {
                if (!request.session.walls[wallId].wall_user_id) {
                    me.Controller.ensureAnonymousUser(request, null, (userId) => {
                        request.session.walls[wallId].wall_user_id = userId;
                        request.session.walls[wallId].is_anonymous = true;
                        request.session.walls[wallId].is_wechat = request.session.is_wechat;
                        callback(null, userId);
                    });
                } else {
                    callback(null, request.session.walls[wallId].wall_user_id);
                }
            },
            /*(wallUserId, callback) => {
                if (url.indexOf('mp.weixin.qq.com/s') >= 0) {
                    me.Controller.fetchContent(url, (content) => {
                        content = content.replace(/data-src/g, 'src').replace(/\\n/g, "");
                        url = JSON.stringify(`data:text/html;charset=utf-8,${content}`);
                        callback(null, wallUserId);
                    });
                } else {
                    callback(null, wallUserId);
                }
            },*/
            (wallUserId, callback) => {
                me.Controller.updateAssetStatistics(assetId, wallUserId, 'click', null, (result) => {
                    console.log(result);
                });

                me.Controller.getAssetStatisticsById(assetId, (statistics) => {
                    let isLiked = (statistics && statistics.likes && statistics.likes.length > 0 && statistics.likes.indexOf(wallUserId) > -1);
                    let likeCount = statistics && statistics.likes && statistics.likes.length || 0;
                    callback(null, {
                        like: {
                            is_liked: isLiked,
                            like_count: likeCount
                        }
                    });
                });
                
            },
            (actions, callback) => {
                me.Controller.getWallById(wallId, (wall) => {
                    wall.url = `/wall/${wall.id}`;
                    let data = {
                        wall: wall,
                        url: url,
                        encoded_url: btoa(url).replace(/\//g, '-'),
                        asset_id: assetId,
                        is_wechat: request.session.is_wechat,
                        actions: actions
                    };
                    if (request.session.is_wechat) {
                        me.Controller.getWeChatJssdkConfig(wall.app_id, request, (config) => {
                            data.config = config;
                            response.render('common/mkprobe', data);
                        });
                    } else {
                        response.render('common/mkprobe', data);
                    }
                });
            }
        ]);
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = MarketingProbeHandler;